package com.magicode.cms.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IEntityChangeHistoryDao;
import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.EntityChangeHistory;
import com.magicode.cms.service.IEntityChangeHistoryService;;

@Service("serviceEntityChangeHistory")
public class EntityChangeHistoryServiceImpl extends BaseServiceImpl<EntityChangeHistory,IEntityChangeHistoryDao> implements IEntityChangeHistoryService,UtilPath{

	@Autowired
	@Qualifier("daoEntityChangeHistory")
	private IEntityChangeHistoryDao entityChangeHistoryDao;
	public IEntityChangeHistoryDao getDao() throws Exception {
		return entityChangeHistoryDao;
	}
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "实体名称");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "propertyKey");
		map.put(json_title, "属性字段");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "propertyLable");
		map.put(json_title, "属性标题");
		map.put("width", 90);
		beaninfo.add(map);
		
		map = new HashMap<String, Object>();
		map.put(json_key, "propertyOldValue");
		map.put(json_title, "属性原值");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "operator");
		map.put(json_title, "操作人");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "createTime");
		map.put(json_title, "操作时间");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "ipAddress");
		map.put(json_title, "操作人IP");
		map.put("width", 90);
		beaninfo.add(map);
		return beaninfo;
	}
	@Override
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
	}
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql = " from "+com.magicode.cms.entiy.base.EntityChangeHistory.class.getName()+" t "
				+ " left join t.operator as u "
				+ " where 1 = 1   ";
		if(null!=map && !map.isEmpty()){
			
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id='"+map.get(field_id)+"'";
			}
			if(map.containsKey(field_title)){
				hql = hql+" and "+entity_alias_name+".entityClassLable like '%"+map.get(field_title)+"%'";
			}
			if(map.containsKey("entityId")){
				hql = hql+" and "+entity_alias_name+".entityId='"+map.get("entityId")+"'";
			}
		}
		hql+=" order by t.createTime desc ";
		return hql;
	}
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
			
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere,0, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".entityClassLable as title ");
			gridsel.put("propertyKey", "t.propertyKey as propertyKey ");
			gridsel.put("propertyLable", "t.propertyLable as propertyLable ");
			gridsel.put("propertyOldValue", "t.propertyOldValue as propertyOldValue ");
			gridsel.put("operator", "u.userName as operator ");
			gridsel.put("createTime", "t.createTime as createTime ");
			gridsel.put("ipAddress", "t.ipAddress as ipAddress ");
			List<Map<String, Object>> list= this.queryMapDataList(hql, gridsel,start , size);
			return list;
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}
	
	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		return opts;
	}
	
	public IEntityChangeHistoryDao getEntityChangeHistoryDao() {
		return entityChangeHistoryDao;
	}
	public void setEntityChangeHistoryDao(
			IEntityChangeHistoryDao entityChangeHistoryDao) {
		this.entityChangeHistoryDao = entityChangeHistoryDao;
	}


}
